// pages/campCupdetail/index.js
const http = require("../../api/index")
const requsest = require('../../utils/request')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    campDetail: {},//营地详情
    url: requsest.baseURL,
    type: '1',
    CampCampsiteList: '', //营位
    campsiteId:''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const campId = options.campId
    this.setData({
      userId:wx.getStorageSync('userInfo').userId
    })
    if (campId) {
      this.getCampDetail(campId);
      // this.getEvaluateList(campId)
      this.getFacility(campId);
      this.getSpots(campId)
      this.getCampCampsite(campId);
      this.getProject(campId)
      this.getActivity(campId)
      // this.getCollect(this.data.userId, campId)
    }
  },
  getCampDetail(campId) {
    http.campDetail(campId).then(res => {
      console.log(res);
      // 判断有没有逗号 有就是多张图片
      let item = res.data
      if (item.imageUrl.indexOf(',') != -1) {
        item.images = item.imageUrl.split(',')
      } else {
        item.images = []
        item.images.push(item.imageUrl)
      }
      this.setData({
        campDetail: res.data
      })
    })
  },
  hideCampInfo() {
    this.setData({
      showInfo: false
    })
  },
  showCampInfo() {
    this.setData({
      showInfo: true
    })
  },
  showFacilities() {
    this.setData({
      showFac: true
    })
  },
  hideFacilities() {
    this.setData({
      showFac: false
    })
  },
  getFacility(campId) {
    http.getFacilitiesA(campId).then((res) => {
      if (res.code === 200) {
        this.setData({
          facilityList: res.data
        })
      }
    })
  },
  // 营位
  getCampCampsite(campId) {

    http.getCampCampsite(campId).then((res) => {
      if (res.code === 200) {
        for(let i = 0; i<res.rows.length; i++){
          if(res.rows[i].factor !== null){
            res.rows[i].factors = []
            const list = res.rows[i].factor.split(',')
            for(let j = 0 ; j<list.length ; j++){
              if(list[j] == 1){
                res.rows[i].factors.push("有水")
              }else if(list[j] == 2){
                res.rows[i].factors.push("有电")
              }else if(list[j] == 3){
                res.rows[i].factors.push("无水")
              }else if(list[j] == 4){
                res.rows[i].factors.push("无水")
              }else if(list[j] == 5){
                res.rows[i].factors.push("有新能源车充电桩")
              }else if(list[j] == 6){
                res.rows[i].factors.push("有卫生间")
              }else{
                res.rows[i].factors.push("其他条件")
              }
            }
          }
        }
        this.setData({
          CampCampsiteList: res.rows,
          campsiteId: res.rows.campsiteId
        })
      }
    })
  },
    //点击电话
    goPhone() {
      wx.makePhoneCall({
        phoneNumber: this.data.campDetail.phone,
        success: function () {
          console.log('拨打电话成功！');
        },
        fail: function () {
          console.log('拨打电话失败！');
        }
      });
    },
    // 其他项目
  getProject(campId) {
    http.getProject(campId).then((res) => {
      if (res.code === 200 && res.data) {
        let list = res.data.map(item => {
          return {
            ...item,
            number: 0,
            type: 2,
            unitPrice: 0
          }
        })
        this.setData({
          projectList: list
        })
      }
    })
  },
  // 活动列表
  getActivity(campId) {
    http.getActivity(campId).then((res) => {
      if (res.code === 200 && res.data) {
        let list = res.data.map(item => {
          return {
            ...item,
            number: 0,
            type: 3,
            unitPrice: 0
          }
        })
        this.setData({
          activityList: list
        })
      }
    })
  },
   // 周边景点
   getSpots(campId) {
    http.getSpots(campId).then((res) => {
      if (res.code === 200) {
        this.setData({
          spotsList: res.data
        })
      }
    })
  },
  bindChange(e) {
    const val = e.detail.value
    this.setData({
      month: this.data.months[val[1]],
      day: this.data.days[val[2]],
      isDaytime: !val[3]
    })
  },
    // 点击预约
    toReservation() {
      wx.navigateTo({
        url: `/pages/reservation/reservation?campId=`+this.data.campDetail.campId
      })
    },
    // 发送收藏请求
  toggleFavorite() {
    if (this.data.isCoolingDown) {
      // 如果处于冷却状态，则提示用户
      wx.showToast({
        title: '操作太频繁，请稍后再试',
        icon: 'none'
      });
      return;
    }

    this.setData({
      isCoolingDown: true
    });

    // 根据当前收藏状态决定调用哪个接口
    if (this.data.isFavorited) {
      // 取消收藏
      this.cancelFavorite();
    } else {
      // 收藏
      this.addFavorite();
    }
  },
   //添加收藏
   addFavorite: function () {
    // 发送收藏请求
    http.collectCamp({
      campId: this.data.campDetail.campId,
      userId: this.data.userId
    }).then(res => {
      // 成功后更新状态
      this.setData({
        isFavorited: true,
        isCoolingDown: false
      });
      // 提示用户
      wx.showToast({
        title: '收藏成功',
        icon: 'success'
      })
    })
  },

  //取消收藏
  cancelFavorite: function () {
    // 发送取消收藏请求
    http.removeCollectCamp({
      campId: this.data.campDetail.campId,
      userId: this.data.userId
    }).then(res => {
      // 成功后更新状态
      this.setData({
        isFavorited: false,
        isCoolingDown: false
      });

      // 提示用户
      wx.showToast({
        title: '取消收藏成功',
        icon: 'success'
      })
    })
  },

  // 是否收藏
  getCollect(userId, campId) {
    http.collectList(userId).then((res) => {
      if (res.code === 200) {
        for (let i = 0; i < res.data.length; i++) {
          if (res.data[i].campId === campId) {
            this.setData({
              isFavorited: true,
              isCoolingDown: false
            });
          }
        }
      }
    })
  },
  // 去评价
  toEvaluate() {
    wx.navigateTo({
      url: `/pages/evaluate/evaluate?campId=${this.data.campDetail.campId}`
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})